
ARCH=`uname -m`


SED() {
sed -i 's/$TAG//g' $PRGNAM.SlackBuild
sed -i "s/tgz/$TGZ_TXZ/g" $PRGNAM.SlackBuild
}


DOWNLOAD_BUILD() {
  download=0
  [ -e $TOTAL_DIR/README ] || download=1
#  [ -e $TOTAL_DIR/doinst.sh ] || download=1
  [ -e $TOTAL_DIR/slack-desc ] || download=1
  [ -e $TOTAL_DIR/$PRGNAM.SlackBuild ] || download=1
  [ -e $TOTAL_DIR/$PRGNAM.info ] || download=1
  
  if [ "$download" == "1" ]; then
    wget -c ftp://slackbuilds.org/$SLACK_VERSION/$CLASSE/$PRGNAM.tar.gz
    tar -xzvf $DIRPORTS/$CLASSE/$PRGNAM/$PRGNAM.tar.gz -C ../
  fi

  cd $TOTAL_DIR
  source $TOTAL_DIR/$PRGNAM.info
  #PRGNAM=`more $PRGNAM.info | grep PRGNAM | cut -d= -f2 | sed s/\"//g`
  #VERSION=`more $PRGNAM.info | grep VERSION | cut -d= -f2 | sed s/\"//g`
  #LINK1=`more $PRGNAM.info | grep DOWNLOAD_x86_64= | cut -d= -f2 | sed s/\"//g`
  #LINK2=`more $PRGNAM.info | grep DOWNLOAD= | cut -d= -f2 | sed s/\"//g`
  chmod +x $TOTAL_DIR/$PRGNAM.SlackBuild
  SED
}


DOWNLOAD_SOURCE() {
  if [ "$ARCH" == "x86_64" ]; then
    if [ "$DOWNLOAD_x86_64" == "" ] || [ "$DOWNLOAD_x86_64" == "UNSUPPORTED" ] || [ "$DOWNLOAD_x86_64" == "UNTESTED" ];then
      LINK_DOWNLOAD=$DOWNLOAD
    else
      LINK_DOWNLOAD=$DOWNLOAD_x86_64
    fi
  else
    LINK_DOWNLOAD=$DOWNLOAD
  fi

  LINK_DOWNLOAD_FILE=$(echo $LINK_DOWNLOAD | rev | cut -d/ -f1 | rev)
  for i in $LINK_DOWNLOAD; do wget -c $i; done
}


DEPENDS_SOLUTION() {
  for i in $(cat $PORTSDB/$CLASSE/$PRGNAM/$DEP | awk '{print $1}') ; do
    if [ $i != "" ] ; then
      cd $DIRPORTS/$i
      ./$PORTSCRIPT dep || exit 1
      cd $TOTAL_DIR
    fi
  done
}


DEPENDS_SOLUTION_INSTALL() {
  for i in $(cat $PORTSDB/$CLASSE/$PRGNAM/$DEP | awk '{print $1}') ; do
    if [ $i != "" ] ; then
      cd $DIRPORTS/$i
      ./$PORTSCRIPT dep-all || exit 1
      cd $TOTAL_DIR
    fi
  done
}


case $1 in
  -h|help|-help|--help)
      echo "
	Download SlackBuilds script -> Download program
	./$PORTSCRIPT

	Download SlackBuilds script -> Download program -> run SlackBuilds script
	./$PORTSCRIPT make

	Run SlackBuilds script
	./$PORTSCRIPT makepkg

	Install Package
	./$PORTSCRIPT installpkg

	Download SlackBuilds script -> Download program -> run SlackBuilds script -> Install the package
	./$PORTSCRIPT all

	Download SlackBuilds script -> Download program -> Resolve dependencies
	./$PORTSCRIPT depends

	Download SlackBuilds script -> Download program -> Resolve dependencies -> run SlackBuilds script -> Install the package
	./$PORTSCRIPT depends-all

	Clean
	./$PORTSCRIPT clean
	  clear up the current directory
	./$PORTSCRIPT clean distifiles
	  clean up the directory DISTIFILES

	Deletar o pacote
	./$PORTSCRIPT deletepkg

	Remover o pacote instalado
	./$PORTSCRIPT removepkg

	Exibir logs
	./$PORTSCRIPT log
	    "
      exit 1
      ;;

  clean)
      case $2 in
	distifiles|DISTFILES)
	  rm -rf $DIRPORTS/DISTFILES/*
	  exit 1
	  ;;
	"")
	  for i in `ls | grep -v $PORTSCRIPT`; do  rm -rf $TOTAL_DIR/$i ; done
	  exit 1
	  ;;
	*)
	  echo "Parametro invalido"
	  exit 1
	  ;;
      esac
      ;;

  log|LOG|logs|LOGS)
      more $DIRPORTS/DISTFILES/package_installed
      exit 1
      ;;

  basic)
      DOWNLOAD_BUILD
      exit 1
      ;;

  deletepkg)
      rm -rfv /tmp/$PRGNAM-$VERSION-*-*.$TGZ_TXZ
      exit 1
      ;;

  removepkg)
      removepkg $PRGNAM-$VERSION-*
      exit 1
      ;;

  make) 
      DOWNLOAD_BUILD || exit 1
      DOWNLOAD_SOURCE || exit 1
      $TOTAL_DIR/$PRGNAM.SlackBuild
      exit 1
      ;;

  makepkg)
      $TOTAL_DIR/$PRGNAM.SlackBuild
      exit 1
      ;;

  installpkg) 
      installpkg /tmp/$PRGNAM-$VERSION-$ARCH-*.$TGZ_TXZ || exit 1
      echo `date +%d/%m/%Y_%H:%M` `ls /tmp/$PRGNAM-$VERSION-*-*.$TGZ_TXZ` >> $DIRPORTS/DISTFILES/package_installed
      exit 1
      ;;

  all) 
      DOWNLOAD_BUILD || exit 1
      DOWNLOAD_SOURCE || exit 1
      $TOTAL_DIR/$PRGNAM.SlackBuild || exit 1
      installpkg /tmp/$PRGNAM-$VERSION-`uname -m`-*.$TGZ_TXZ || exit 1
      echo `date +%d/%m/%Y_%H:%M` `ls /tmp/$PRGNAM-$VERSION-*-*.$TGZ_TXZ` >> $DIRPORTS/DISTFILES/package_installed
      exit 1
      ;;

  dep)
    DEPENDS_SOLUTION || exit 1
    DOWNLOAD_BUILD || exit 1
    DOWNLOAD_SOURCE || exit 1
    exit 1
    ;;

  dep-all)
    DEPENDS_SOLUTION_INSTALL || exit 1
    DOWNLOAD_BUILD || exit 1
    DOWNLOAD_SOURCE || exit 1
    $TOTAL_DIR/$PRGNAM.SlackBuild  || exit 1
    installpkg /tmp/$PRGNAM-$VERSION-*-*.$TGZ_TXZ || exit 1
    echo `date +%d/%m/%Y_%H:%M` `ls /tmp/$PRGNAM-$VERSION-*-*.$TGZ_TXZ` >> $DIRPORTS/DISTFILES/package_installed || exit 1
    echo "Package installed"
    exit 1
    ;;
esac


exit 0